package com.youease.chris.control
{
	import com.youease.chris.view.CreateMapTitleWindow;
	import com.youease.chris.view.CreateProjectTitleWindow;
	import com.youease.chris.view.CreateTextureTitleWindow;
	import com.youease.chris.view.OpenProjectTitleWindow;
	import com.youease.chris.view.TextureCutTitleWindow;
	import com.youease.chris.view.mediator.CreateMapMediator;
	import com.youease.chris.view.mediator.CreateProjectMediator;
	import com.youease.chris.view.mediator.CreateTextureMediator;
	import com.youease.chris.view.mediator.MapEditMediator;
	import com.youease.chris.view.mediator.OpenProjectMediator;
	import com.youease.chris.view.mediator.TextureCutMediator;
	
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	public class MenuBarCommand extends SimpleCommand implements ICommand
	{
		public static const MENU_CHANGE:String = "menu_change";
		
		public function MenuBarCommand()
		{
			super();
		}
		
		override public function execute(notification:INotification):void {
			var mainApp:MapEdit = (this.facade.retrieveMediator(MapEditMediator.NAME) as MapEditMediator).mapEdit;
			
			var seletedItem:XML = notification.getBody() as XML;
			var data:String = seletedItem.@data;
			switch(data) {
				case "newProject":
					var createProject:Object = PopUpManager.createPopUp(mainApp,CreateProjectTitleWindow,false);
					this.facade.registerCommand(CreateProjectCommand.CREATE_PROJECT,CreateProjectCommand);
					this.facade.registerMediator(new CreateProjectMediator(createProject));
					break;
				case "openProject":
					var openProject:Object = PopUpManager.createPopUp(mainApp,OpenProjectTitleWindow,false);
					this.facade.registerCommand(OpenProjectCommand.OPEN_PROJECT,OpenProjectCommand);
					this.facade.registerMediator(new OpenProjectMediator(openProject));
					break;
				case "checkProject":
					this.facade.registerCommand(SaveProjectCommand.SAVE_PROJECT,SaveProjectCommand);
					sendNotification(SaveProjectCommand.SAVE_PROJECT,null,SaveProjectCommand.CHECK_PROJECT);
					break;
				case "saveProject":
					this.facade.registerCommand(SaveProjectCommand.SAVE_PROJECT,SaveProjectCommand);
					sendNotification(SaveProjectCommand.SAVE_PROJECT);
					break;
				case "newMap":
					var createMap:Object = PopUpManager.createPopUp(mainApp,CreateMapTitleWindow,false);
					this.facade.registerCommand(CreateMapCommand.CREATE_MAP,CreateMapCommand);
					this.facade.registerMediator(new CreateMapMediator(createMap));
					break;
				case "newTexture":
					var createTexture:Object = PopUpManager.createPopUp(mainApp,CreateTextureTitleWindow,false);
					this.facade.registerCommand(CreateTextureCommand.CREATE_TEXTURE,CreateTextureCommand);
					this.facade.registerMediator(new CreateTextureMediator(createTexture));
					break;
				case "textureView":
					if(!this.facade.hasMediator(TextureCutMediator.NAME)) {
						var textureCut:Object = PopUpManager.createPopUp(mainApp.mapScene,TextureCutTitleWindow,false);
						this.facade.registerMediator(new TextureCutMediator(textureCut));
						this.sendNotification(TextureCutMediator.TEXTURE_CUT);
					}else {
						var textureCutMediator:TextureCutMediator = this.facade.retrieveMediator(TextureCutMediator.NAME) as TextureCutMediator;
						textureCutMediator.textureCut.open();
					}
					break;
			}
		}
	}
}